package com.uyaer.engine.utils
{
	import com.uyaer.engine.Const;
	import com.uyaer.engine.loader.URI;
	import com.uyaer.engine.plug.JSON;
	
	import flash.geom.Rectangle;

	public class Utils
	{
		/**
		 *随机产生从min - max的整数 
		 * @param min
		 * @param max
		 * @return 
		 * 
		 */		
		public static function rangInt(min:int , max:int):int{
			return Math.ceil(Math.random() * (max - min)) + min;
		}
		
		/**
		 *随机产生1-dig的随机数，位数不够补0 
		 * @param dig
		 * @return 
		 * 
		 */		
		public static function rangString(dig:uint):String{
			dig = dig || 1;
			var max:uint = (dig + 1) * 10 - 1;
			var min:uint = 0;
			var r:String = rangInt(min , max).toString();
			if(r.length < dig){
				var temp:int = dig - r.length;
				var tempStr:String = "";
				for(var i:int = 0 ; i < temp ; i ++){
					tempStr += "0";
				}
				tempStr += r;
				return tempStr;
			}
			return r;
		}
		
		/**
		 *用 “0” 补全数值字符串 
		 * @param num
		 * @param dig
		 * @return 
		 * 
		 */		
		public static function complement(num:uint , dig:uint):String{
			var str:String = num.toString();
			if(str.length < dig){
				var temp:String = "";
				for(var i:int = 0 ; i < (dig - str.length); i++){
					temp += "0";
				}
				temp += str;
				return temp;
			}
			return str;
		}
		
		public static function now():Number{
			return new Date().time;
		}
		
		/**
		 *从4方向转化为1为横向左边 
		 * @param dir
		 * @return 
		 * 
		 */		
		public static function axis2Dir(dir:int):Array{
			var arr:Array = [0 , 0];
			if(dir == Const.LEFT){
				arr[0] = -1;
			}else if(dir == Const.RIGHT){
				arr[0] = 1;
			}else if(dir == Const.TOP){
				arr[1] = -1;
			}else if(dir == Const.BOTTOM){
				arr[1] = 1;
			}
			return arr;
		}
		
		/**
		 *解析texture packer的数据 
		 * @param frames
		 * @return 
		 * 
		 */		
		public static function texturePacker(data:String):HashMap{
			var json:Object = JSON.parse(data);
			var map:HashMap = new HashMap();
			var o:Object;
			for each (var e:Object in json.frames){
				o = e.frame;
				map.put(e.filename,new Rectangle(o.x,o.y,o.w,o.h));
			}
			return map;
		}
	}
}